<template>
  <div class="add-wrapper">
    <Dialog ref="dialog" class="dialog-wrapper" @closeDialog="handleConfirm('ruleForm')">
      <template>
        <div>
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            :rules="rules"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="父级分类：">
              <el-select v-model="value" placeholder="请选择" clearable @change="handleBrandChange">
                <el-option
                  v-for="item in list"
                  :key="item.id"
                  :label="item.kind_name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="分类名称：" prop="kind_name">
              <el-input v-model="ruleForm.kind_name" placeholder="最多5个字" />
            </el-form-item>
            <el-form-item label="分类图片：" prop="style">
              <el-upload
                :limit="1"
                action="https://wx.chengkenet.com/api/common/upload/"
                list-type="picture-card"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
                :on-success="handleSuccess"
                :file-list="fileList"
              >
                <div slot="tip" class="el-upload__tip color-red">（建议尺寸：110px*110px）</div>
                <i class="el-icon-plus" />
              </el-upload>
              <el-dialog :visible.sync="dialogVisible">
                <img width="100%" :src="ruleForm.style" alt>
              </el-dialog>
            </el-form-item>
          </el-form>
        </div>
      </template>
    </Dialog>
  </div>
</template>
<script>
import Dialog from '@/common/dialog'
const Err_OK = 200
export default {
  components: {
    Dialog
  },
  props: {
    option: {
      type: String,
      default: ''
    },
    list: {
      type: Array,
      default: () => []
    },
    listItem: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      fileList: [],
      value: '',
      dialogVisible: false,
      ruleForm: {
        higherup: '',
        kind_name: '',
        style: ''
      },
      rules: {
        kind_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 1, max: 5, message: '限5个字', trigger: 'blur' }
        ]
      },
      id: ''
    }
  },
  watch: {
    option(newVal) {
      if (newVal === 'add') {
        this.value = ''
      }
      this.clear()
    },
    listItem(newVal, oldVal) {
      if (this.option === 'edit') {
        this.$nextTick(() => {
          this.value = newVal.kind_name
        })
        this.ruleForm.kind_name = newVal.kind_name
        this.ruleForm.id = newVal.id
        this.ruleForm.style = newVal.style
        this.fileList = [this.handleList(newVal.style)]
      } else if (this.option === 'addChildren') {
        this.value = newVal.kind_name
        this.ruleForm.higherup = newVal.id
      }
    }
  },
  methods: {
    // 图片、视频格式化
    handleList(url) {
      const oIndex = url.lastIndexOf('/')
      const name = url.slice(oIndex + 1)
      return { name, url }
    },
    show() {
      this.$refs.dialog.show()
    },
    handleRemove(file, fileList) {
      this.ruleForm.style = ''
    },
    handlePictureCardPreview(file) {
      this.ruleForm.style = file.url
      this.dialogVisible = true
    },
    handleSuccess(response) {
      if (response.code === Err_OK) {
        this.ruleForm.style = response.data.file
      }
    },
    handleBrandChange(id) {
      this.ruleForm.higherup = id
    },
    handleConfirm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('confirm', this.ruleForm)
          this.clear()
        } else {
          this.$refs.dialog.show()
          return false
        }
      })
    },
    clear() {
      this.ruleForm = {
        higherup: '',
        kind_name: '',
        style: ''
      }
      if (this.option !== 'edit') {
        this.fileList = []
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.el-input /deep/ .el-input__inner {
  width: 195px;
  height: 36px;
  border-radius: 3px;
  border: 1px solid rgba(226, 229, 234, 1);
}
.el-form-item /deep/ .el-icon-zoom-in {
  font-size: 26px;
  color: #fff;
}
.el-form-item /deep/ .el-icon-delete {
  font-size: 26px;
  color: #fff;
}
.color-red {
  color: red;
}
.dialog-wrapper /deep/ .el-dialog {
  min-width: 530px;
}
</style>
